﻿/*+===================================================================
File:      BossTwoPartArm.cpp

Summary:   Định nghĩa các phương thức của CBossTwoPartArm.
===================================================================+*/

#include "Global.h"
#include "BossTwoPartArm.h"
#include "PlayState.h"

CBossTwoPartArm::CBossTwoPartArm(CBill* bill, D3DXVECTOR2 pos, int id, int typeId) : CMoveableObject(pos, id, typeId)
{
	if (typeId == ID_BOSS_TWO_ARM_HEAD)
	{
		m_pGun = new CGun(ID_BOSS_TWO_BULLET, 1, 1);
		m_bVisible = true;
		m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_BOSS_TWO_ARM_HEAD);
		m_nLife = 10;
	}
	else if (typeId == ID_BOSS_TWO_ARM_BODY)
	{
		m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_BOSS_TWO_ARM_BODY);
		m_bVisible = false;
	}
	
	m_bill = bill;
	m_fR = 0.0f;
	m_nStatus = STATUS_START;

	m_box.height = 32;
	m_box.width = 32;
}

CBossTwoPartArm::~CBossTwoPartArm()
{
}

void CBossTwoPartArm::Update(float timeFrame)
{
	if (!m_bIsLive)
		return;

	UpdateBox(timeFrame);
}

void CBossTwoPartArm::UpdateCollision(vector<CGameObject*> vecGameObjs, float timeFrame)
{
	if (!m_bIsLive || m_nTypeId != ID_BOSS_TWO_ARM_HEAD || m_nStatus == STATUS_START)
		return;

	float timeCollision = timeFrame;
	CGameObject* objCollision = NULL; // Đối tượng va chạm sớm nhất.
	float normalx = 0.0f, normaly = 0.0f;

	// Xử lí va chạm với đạn của Bill
	for (int i = 0; i < CPlayState::s_billBullets.size(); i++)
	{
		if (CPlayState::s_billBullets[i]->IsLive())
		{
			float t = CheckCollision(CPlayState::s_billBullets[i], normalx, normaly, timeFrame);

			if (t < timeFrame && t < timeCollision)
			{
				// Tìm đối tượng và thời gian va chạm sớm nhất.
				timeCollision = t;
				objCollision = CPlayState::s_billBullets[i];
			}
		}
	}

	// Xử lí va chạm
	if (objCollision != NULL)
	{
		this->Kill();
		objCollision->Kill();
	}


}

void CBossTwoPartArm::Draw()
{
	if (m_bVisible)
		CMoveableObject::Draw();
}

bool CBossTwoPartArm::Kill()
{
	if (m_nLife > 1)
		m_nLife--;
	else
	{
		m_bIsLive = false;
		CExplosion* explosion = new CExplosion(m_pos, 0, ID_SMALL_EXPLOSION);
		CPlayState::s_objsOutSideQuadTree.push_back(explosion);

		if (prevPartArm != NULL)
			prevPartArm->Kill();
	}

	return true;
}

void CBossTwoPartArm::SetLife(int life)
{
	m_nLife = life;
}

void CBossTwoPartArm::SetVisible(bool visible)
{
	m_bVisible = visible;
}

void CBossTwoPartArm::ChangeStatus(int status)
{
	m_nStatus = status;
	m_nCount = 0;

	switch (m_nStatus)
	{
		case STATUS_UP_DOWN:
			m_timeFire = 1.3f;
				break;
		case STATUS_END_ROTATE:
			m_timeChange = 0.5f;
				break;
		case STATUS_WING:
			m_timeChange = 2.0f;
			m_timeFire = 1.3f;
			break;
		case STATUS_FOLLOW:
			m_timeChange = 4.0f;
		default:
			break;
	}
}

int CBossTwoPartArm::GetId()
{
	return m_nId;
}

void CBossTwoPartArm::Fire()
{
	if (m_nTypeId == ID_BOSS_TWO_ARM_HEAD)
	{
		D3DXVECTOR2 billPos = m_bill->GetPos();
		float angleFire = atan2(billPos.y - m_pos.y, billPos.x - m_pos.x) * 180 / PI;

		m_pGun->SetBulletAngle(angleFire);
		m_pGun->Fire(m_pos);
	}
}

int CBossTwoPartArm::GetStatus()
{
	return m_nStatus;
}